package com.example.tests;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class TestBase {

	private static WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();

	@BeforeTest
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@AfterTest
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	protected void createGroup(GroupObject groupObject) {
		openMainPage();
		goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		returnToGroupListPage();
	}

	private void returnToGroupListPage() {
		driver.findElement(By.linkText("group page")).click();
	}

	private void submitGroupCreation() {
		driver.findElement(By.name("submit")).click();
	}

	private void fillGroupForm(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);
	}

	private void type(By locator, String text) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(text);
	}

	private void initGroupCreation() {
		driver.findElement(By.name("new")).click();
	}

	private void goToGroupListPage() {
		driver.findElement(By.linkText("groups")).click();
	}

	public void openMainPage() {
		driver.get("http://localhost/addressbookv4.1.4/");
	}

	protected void createContact(ContactObject contactObject) {
		openMainPage();
		initContactCreation();
		fillContactForm(contactObject);
		submitContactCreation();
		returnToHomePage();
	}

	private void returnToHomePage() {
		driver.findElement(By.linkText("home page")).click();
	}
	
	private void submitContactCreation() {
		driver.findElement(By.name("submit")).click();
	}	
	
	private void fillContactForm(ContactObject contactObject) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		select(By.name("bday"), contactObject.bday);
		select(By.name("bmonth"), contactObject.bmonth);
		type(By.name("byear"), contactObject.byear);
		select(By.name("new_group"), contactObject.new_group);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);
	}

	protected void select(By locator, String text) {
		new Select(driver.findElement(locator)).selectByVisibleText(text);
	}
	
	private void initContactCreation() {
		driver.findElement(By.linkText("add new")).click();
	}
	
	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

}
